package com.sckj.security.springmvc.interceptor;

import com.sckj.security.springmvc.model.UserDto;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class SimpleAuthenticationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //读取会话信息
        Object object=request.getSession().getAttribute(UserDto.SESSION_USER_KEY);
        if(object==null){
            writeContent(response,"请登录");
        }
        UserDto user=(UserDto) object;

        //请求URL地址
        String requestURI=request.getRequestURI();
        //如果具备权限1，就放行1
        if(user.getAuthorities().contains("p1") && requestURI.contains("/r1")){
            return true;
        }

        //如果具备权限1，就放行2
        if(user.getAuthorities().contains("p2") && requestURI.contains("/r2")){
            return true;
        }
        writeContent(response,"权限不足,拒绝访问");
        return false;
    }

    //响应输出
    private void writeContent(HttpServletResponse response, String msg) throws IOException {
        response.setContentType("text/html;charset=utf‐8");
        PrintWriter writer = response.getWriter();
        writer.print(msg);
        writer.close();
        response.resetBuffer();
    }
}
